package com.zhkj.framework.handle;

import com.zhkj.framework.exception.CommonException;
import com.zhkj.framework.result.ResultVO;
import com.zhkj.framework.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @Auther: zhangy
 * @Date: 2018/10/17 14:56
 * @Description:自定义Exception处理类
 *
 */
@Slf4j
@ControllerAdvice
public class ExceptionHandle  {

    //捕获系统异常
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultVO handle(Exception e){

        if(e instanceof CommonException){
            CommonException commonException = (CommonException) e;
            return ResultVOUtil.error(commonException.getCode(),commonException.getMessage());
        }else {
            log.error("【系统异常】:{}",e);
            return ResultVOUtil.error(-1,e.getMessage());
        }
    }


}
